package com.kinon.bs_app.fragments;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.bs_app.R;
import com.kinon.bs_app.activities.LoginActivity;
import com.kinon.bs_app.activities.MessageActivity;
import com.kinon.bs_app.beans.UserBean;
import com.kinon.bs_app.beans.UserInfo;
import com.kinon.bs_app.utils.HttpUtils;
import com.kinon.bs_app.utils.PrefUtils;
import com.kinon.bs_app.utils.http.StrCallback;

import retrofit2.Call;
import retrofit2.Response;

public class WodeFragment extends Fragment implements View.OnClickListener {

    private View mView = null;

    private ImageView iv_face = null;
    private TextView tv_phone = null;
    private TextView tv_level = null;

    private TextView tv_rest_days = null;
    private TextView tv_rest_date = null;

    private Button btn_xufei = null;
    private Button btn_xiaoxi = null;
    private Button btn_jqqd = null;

    private LinearLayout ll_ivFriends = null;
    private LinearLayout ll_submit_mind = null;
    private LinearLayout ll_add_group = null;
    private LinearLayout ll_update = null;

    private LinearLayout ll_exit = null;

    private static String INVITE_CODE = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_wode, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        iv_face = mView.findViewById(R.id.iv_face);
        tv_phone = mView.findViewById(R.id.tv_phone);
        tv_level = mView.findViewById(R.id.tv_level);

        tv_rest_days = mView.findViewById(R.id.tv_rest_days);
        tv_rest_date = mView.findViewById(R.id.tv_rest_date);

        btn_xufei = mView.findViewById(R.id.btn_xufei);
        btn_xiaoxi = mView.findViewById(R.id.btn_xiaoxi);
        btn_jqqd = mView.findViewById(R.id.btn_jqqd);

        btn_xufei.setCompoundDrawables(null,
                stardardImg(R.drawable.img_xufei),null,null);
        btn_xiaoxi.setCompoundDrawables(null,
                stardardImg(R.drawable.img_xiaoxi), null,null);
        btn_jqqd.setCompoundDrawables(null,
                stardardImg(R.drawable.img_jqqd), null,null);

        ll_ivFriends = mView.findViewById(R.id.ll_ivFriends);
        ll_submit_mind = mView.findViewById(R.id.ll_submit_mind);
        ll_add_group = mView.findViewById(R.id.ll_add_group);
        ll_update = mView.findViewById(R.id.ll_update);
        ll_exit = mView.findViewById(R.id.ll_exit);

    }

    private void initListener() {
        btn_xufei.setOnClickListener(this);
        btn_xiaoxi.setOnClickListener(this);
        btn_jqqd.setOnClickListener(this);

        ll_ivFriends.setOnClickListener(this);
        ll_submit_mind.setOnClickListener(this);
        ll_add_group.setOnClickListener(this);
        ll_update.setOnClickListener(this);
        ll_exit.setOnClickListener(this);
    }

    private void initData() {
        getUserInfo();
    }

    /**
     * 计算时间（天数）
     * @param start
     * @param end
     */
    private int getRestDays(String start, String end) {
        return 10;
    }

    /**
     * 获取个人资料
     */
    private void getUserInfo() {
        Call<String> call = HttpUtils.getRequestApi().userinfo(PrefUtils.getToken());
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Log.d("zzy", "个人资料：" + data);

                Gson gson = new Gson();
                UserBean bean = gson.fromJson(data, UserBean.class);
                UserInfo info = bean.getUser();

                tv_phone.setText(info.getPhone());
                tv_level.setText("LV  " + info.getVip());

                String start = info.getRegist_time();
                String end = info.getExpire_time();
                int days = getRestDays(start, end);
                if (days < 0) {
                    days = 0;
                }
                tv_rest_days.setText(days + "");
                tv_rest_date.setText(end.substring(0, 11));
                INVITE_CODE = info.getInvite_code();
            }

            @Override
            public void onError(int code, String error) {
                Log.d("zzy", "error: " + error);
            }
        });
    }

    /**
     * 获取套餐
     */
    private void getPackage() {
        Call<String> call = HttpUtils.getRequestApi().getPackage(PrefUtils.getToken());
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                Log.d("zzy", "VIP套餐：" + data);
            }

            @Override
            public void onError(int code, String error) {
                Log.d("zzy", "error: " + error);
            }
        });
    }

    private void showMsg() {
        Intent intent = new Intent(getActivity(), MessageActivity.class);
        startActivity(intent);
    }

    private void inviteFriends() {
        // 复制邀请码到粘贴板
        String msg = "币圈稳定盈利，注册就送1000元红包，我的邀请码" + INVITE_CODE +
                " http://besure.ogtchain.com/#/passport/register?code=" + INVITE_CODE;
        ClipboardManager clipboardManager = (ClipboardManager)
                getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setText(msg);
//        Toast.makeText(getActivity(), "信息已复制到剪切板", Toast.LENGTH_SHORT).show();
        Snackbar.make(mView, "信息已复制到剪切板", Snackbar.LENGTH_SHORT).show();
    }

    private void subMind() {
        // 反馈意见
    }

    private void addGroup() {
        // 加入官方群
    }

    private void updateApp() {
        // 版本更新
    }

    /**
     * 退出登录
     */
    private void exitLogin() {
        PrefUtils.remove("token");
        PrefUtils.remove("password");
        Intent intent = new Intent(mView.getContext(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_xufei:
//                getPackage();
                break;
            case R.id.btn_xiaoxi:
                showMsg();
                break;
            case R.id.btn_jqqd:
                break;

            case R.id.ll_ivFriends:
                inviteFriends();
                break;
            case R.id.ll_submit_mind:
                subMind();
                break;
            case R.id.ll_add_group:
                addGroup();
                break;
            case R.id.ll_update:
                updateApp();
                break;
            case R.id.ll_exit:
                exitLogin();
                break;
            default:
                break;
        }
    }

    public Drawable stardardImg(int position) {
        Drawable drawable = getResources().getDrawable(position);
        drawable.setBounds(0, 0, 80, 80);
        return drawable;
    }
}
